package org.groupg.project;

import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.MutableIntIntMap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.factory.Multimaps;
import org.eclipse.collections.impl.factory.primitive.IntIntMaps;
import org.eclipse.collections.impl.factory.primitive.IntLists;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class EclipseCollectionsDemo {

    public static void main(String[] args) {
        // 1. 原始类型优化集合
        MutableIntList intList = IntLists.mutable.of(3, 1, 4, 1, 5, 9, 2);
        System.out.println("原始整数列表: " + intList);

        // 避免装箱的排序
        intList.sortThis();
        System.out.println("排序后: " + intList);

        // 原始类型映射
        MutableIntIntMap intMap = IntIntMaps.mutable.empty();
        intList.forEach(each -> intMap.addToValue(each, 1));
        System.out.println("元素计数: " + intMap);

        // 2. 丰富的高级集合操作
        MutableList<String> names = Lists.mutable.of(
            "Alice", "Bob", "Charlie", "David", "Eva", "Frank");

        // 链式函数式操作
        MutableList<String> result = names
            .select(name -> name.length() > 4)
            .collect(String::toUpperCase)
            .toSortedList();
        System.out.println("过滤并转换: " + result);

        // 3. 多值集合 (Multimap)
        MutableMultimap<String, String> departmentMap = Multimaps.mutable.list.empty();
        departmentMap.put("Engineering", "Alice");
        departmentMap.put("Engineering", "Bob");
        departmentMap.put("Marketing", "Charlie");
        departmentMap.putAll("HR", Lists.mutable.of("David", "Eva"));

        System.out.println("工程团队成员: " + departmentMap.get("Engineering"));
        System.out.println("所有部门: " + departmentMap.keySet());

        // 4. Bag集合（计数）
        MutableBag<String> wordBag = Bags.mutable.empty();
        String text = "the quick brown fox jumps over the lazy dog";
        for (String word : text.split(" ")) {
            wordBag.add(word);
        }

        System.out.println("单词计数:");
        wordBag.forEachWithOccurrences((word, count) ->
            System.out.println(word + ": " + count));

        // 5. 并行处理优化
        MutableList<Integer> numbers = Lists.mutable.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        long start = System.nanoTime();
        int parallelSum = Math.toIntExact(numbers.asParallel(executorService, 2)
                .select(i -> i % 2 == 0)
                .sumOfInt(i -> i * 2));
        long duration = System.nanoTime() - start;

        System.out.println("并行计算结果: " + parallelSum);
        System.out.println("并行计算耗时: " + duration + " ns");
    }
}